package com.will.network.images.round;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Xfermode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

import com.will.network.R;


public class HoverImageView extends ImageView {
	private static final ImageView.ScaleType SCALE_TYPE = ImageView.ScaleType.CENTER_CROP;
	private static final PorterDuffXfermode duffMode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
	
	private boolean pressed;
	private int borderColor = 0x44000000;
	private int hoverColor = 0x22000000;
	private Paint borderPaint;
	private Path boundPath;
	private Path borderPath;
	private RectF rect = new RectF();
	private float borderWidth = 1f;
	public int isShowVip;
	public HoverImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		setup(attrs);
	}

	public HoverImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		setup(attrs);
	}

	public HoverImageView(Context context) {
		super(context);
		setup(null);
	}
	public void setIsShowVip(int isShowVip){
		this.isShowVip=isShowVip;
	}
	public  int getIsShowVip(){
		return this.isShowVip;
	}
	private void setup(AttributeSet attrs) {
		if(attrs != null){
			TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.RoundImageView);
			borderColor = typedArray.getColor(R.styleable.RoundImageView_rivBorderColor, borderColor);
			hoverColor = typedArray.getColor(R.styleable.RoundImageView_rivHoverColor, hoverColor);
			borderWidth = typedArray.getDimension(R.styleable.RoundImageView_rivBorderWidth, borderWidth);
			typedArray.recycle();
		}
		
		borderPath = new Path();
		boundPath = new Path();
		borderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		borderPaint.setStrokeWidth(borderWidth);
		
		super.setScaleType(SCALE_TYPE);
	}
	
	@Override
	protected void onDetachedFromWindow() {
		super.onDetachedFromWindow();
	}
	
	protected void drawBorder(Canvas canvas) {
		borderPaint.setStyle(Style.STROKE);
		borderPaint.setColor(borderColor);

		canvas.drawPath(borderPath, borderPaint);
//		Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_v48);
//		canvas.drawBitmap(bitmap,borderPath);
	}

	protected void drawHover(Canvas canvas) {
		if(this.isClickable() && pressed){
			borderPaint.setStyle(Style.FILL);
			borderPaint.setColor(hoverColor);
			canvas.drawPath(boundPath, borderPaint);
		}
	}
	
	public void buildBorderPath(Path borderPath) {
		borderPath.reset();
		final float halfBorderWidth = borderWidth * 0.5f;
		boundPath.addRect(halfBorderWidth, halfBorderWidth,
				getWidth() - halfBorderWidth, getHeight() - halfBorderWidth, Direction.CW);
	}
	
	public void buildBoundPath(Path boundPath){
		boundPath.reset();
		boundPath.addRect(0, 0, getWidth(), getHeight(), Direction.CW);
	}

	public void setImageUrl(String url){

	}
	
	@Override
	protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
		super.onLayout(changed, left, top, right, bottom);
//		Log.e("yxq,","onLayout"+getIsShowVip());

		if(changed){
			buildBoundPath(boundPath);
			buildBorderPath(borderPath);
		}
	}

	@Override
	protected void onDraw(Canvas canvas) {

//		Log.e("yxq,","Ondraw"+getIsShowVip());
		Drawable maiDrawable = getDrawable();

		if (!isInEditMode() && maiDrawable instanceof BitmapDrawable) {
			Paint paint = ((BitmapDrawable) maiDrawable).getPaint();
	        
	        Rect bitmapBounds = maiDrawable.getBounds();

			int width = bitmapBounds.width();
			int height = bitmapBounds.height();
			if (width < getWidth() || height < getHeight()){
				rect.set(0, 0, getWidth(), getHeight());
			}else{
				rect.set(bitmapBounds);
			}

			int saveCount = canvas.saveLayer(rect, null,
                    Canvas.MATRIX_SAVE_FLAG |
                    Canvas.CLIP_SAVE_FLAG |
                    Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
                    Canvas.FULL_COLOR_LAYER_SAVE_FLAG |
                    Canvas.CLIP_TO_LAYER_SAVE_FLAG);
			getImageMatrix().mapRect(rect);
 
	        paint.setAntiAlias(true);
	        canvas.drawARGB(0, 0, 0, 0);
	        final int color = 0xffffffff;
	        paint.setColor(color);
	        canvas.drawPath(boundPath, paint);
 
			Xfermode oldMode = paint.getXfermode();
			paint.setXfermode(duffMode);
	        super.onDraw(canvas);
	        paint.setXfermode(oldMode);
	        canvas.restoreToCount(saveCount);

	        drawHover(canvas);
			drawBorder(canvas);


			//yxq
//			if(getIsShowVip()==1) {
//				Paint mPaint = new Paint();
//				mPaint.setAntiAlias(true);
//				Bitmap bitmap = null;
//				double w_ = (double) getWidth() / (double) 50;
//				double h_ = (double) getHeight() / (double) 50;
//				int left = 0;
//				int top = 0;
//
//
//				switch (getWidth()) {
//					case 45:
//						bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_v48);
//						bitmap = zoomImage(bitmap, bitmap.getWidth() / 2, bitmap.getHeight()/2);
//						left = getWidth() - (int) (15 * w_);
//						top = getHeight() - (int) (15 * h_);
//						break;
//					case 75:
//						bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_v48);
//						bitmap = zoomImage(bitmap, bitmap.getWidth() / 2, bitmap.getHeight()/2);
//						left = getWidth() - (int) (20 * w_);
//						top = getHeight() - (int) (20 * h_);
//						break;
//					case 80:
//						bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_v80);
//						bitmap = zoomImage(bitmap, bitmap.getWidth() / 2, bitmap.getHeight()/2);
//						left = getWidth() - (int) (16 * w_);
//						top = getHeight() - (int) (16 * h_);
//						break;
//					case 90:
//						bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_v48);
//						bitmap = zoomImage(bitmap, bitmap.getWidth() / 1.5, bitmap.getHeight()/1.5);
//
//						left = getWidth() - (int) (15 * w_);
//						top = getHeight() - (int) (15 * h_);
//						break;
//					case 100:
//						bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_v48);
//						bitmap = zoomImage(bitmap, bitmap.getWidth() / 1.5, bitmap.getHeight()/1.5);
//
//						left = getWidth() - (int) (12 * w_);
//						top = getHeight() - (int) (12 * h_);
//						break;
//					case 120:
//						bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_v60);
//						bitmap = zoomImage(bitmap, bitmap.getWidth() / 1.5, bitmap.getHeight()/1.5);
//
//						left = getWidth() - (int) (15 * w_);
//						top = getHeight() - (int) (15 * h_);
//					break;
//					case 160:
//						bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_v80);
//						bitmap = zoomImage(bitmap, bitmap.getWidth() / 1.5, bitmap.getHeight()/1.5);
//
//						left = getWidth() - (int) (12 * w_);
//						top = getHeight() - (int) (12 * h_);
//						break;
//					case 180:
//						bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_v60);
////						bitmap = zoomImage(bitmap, bitmap.getWidth() / 1.5, bitmap.getHeight()/1.5);
//
//						left = getWidth() - (int) (12 * w_);
//						top = getHeight() - (int) (12 * h_);
//						break;
//					case 240:
//						bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_v80);
//						bitmap = zoomImage(bitmap, bitmap.getWidth() / 1.5, bitmap.getHeight()/1.5);
//
//						left = getWidth() - (int) (12 * w_);
//						top = getHeight() - (int) (12 * h_);
//						break;
//					default:
//						bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_v48);
//						bitmap = zoomImage(bitmap, bitmap.getWidth() / 1.5, bitmap.getHeight()/1.5);
//
//						left = getWidth() - (int) (15 * w_);
//						top = getHeight() - (int) (15 * h_);
//						break;
//
//
//				}
//				if (getWidth() > 240) {
//					bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_v80);
//					left = getWidth() - (int) (15 * w_);
//					top = getHeight() - (int) (15 * h_);
//				}
//				if(getWidth()<45){
//					bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_v48);
//					bitmap = zoomImage(bitmap, bitmap.getWidth() / 2, bitmap.getHeight()/2);
//					left = getWidth() - (int) (20 * w_);
//					top = getHeight() - (int) (20 * h_);
//
//				}
//
//				Log.e("yxq", getWidth() + "===left:" + left + "top:" + top+"canvas:"+canvas.getWidth()+"====="+canvas.getHeight());
//
//				canvas.drawBitmap(bitmap, left, top, mPaint);
//
//			}

			if(getIsShowVip() == 1){
				Paint mPaint = new Paint();
				mPaint.setAntiAlias(true);
				Bitmap bitmap = null;
				float multiple = 0.25f; //v图标相对头像图片的比例
				float left = 0;
				float top = 0;

				float mWidth = getWidth() * multiple;
				float mHeight = getHeight() * multiple;

				left = getWidth() * 0.75f;
				top = getHeight() * 0.75f;

				if(getWidth() <= 60){
					bitmap = zoomImage(BitmapFactory.decodeResource(getResources(), R.drawable.icon_v48), mWidth, mHeight);
				}else if (getWidth() > 60 && getWidth() <= 80){
					bitmap = zoomImage(BitmapFactory.decodeResource(getResources(), R.drawable.icon_v60), mWidth, mHeight);
				}else if (getWidth() > 80 && getWidth() <= 160){
					bitmap = zoomImage(BitmapFactory.decodeResource(getResources(), R.drawable.icon_v80), mWidth, mHeight);
				}else {
					bitmap = zoomImage(BitmapFactory.decodeResource(getResources(), R.drawable.icon_v160), mWidth, mHeight);
				}

				canvas.drawBitmap(bitmap, left, top, mPaint);
			}

		} else {
			super.onDraw(canvas);
		}
	}
	public static Bitmap zoomImage(Bitmap bgimage, double newWidth,
								   double newHeight) {
		// 获取这个图片的宽和高
		float width = bgimage.getWidth();
		float height = bgimage.getHeight();
		// 创建操作图片用的matrix对象
		Matrix matrix = new Matrix();
		// 计算宽高缩放率
		float scaleWidth = ((float) newWidth) / width;
		float scaleHeight = ((float) newHeight) / height;
		// 缩放图片动作
		matrix.postScale(scaleWidth, scaleHeight);
		Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, (int) width,
				(int) height, matrix, true);
		return bitmap;
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		final boolean touched = super.onTouchEvent(event); 
		if(touched){
			switch (event.getAction() & MotionEvent.ACTION_MASK) {
			case MotionEvent.ACTION_DOWN:
				pressed = true;
				postInvalidate();
				break;
			case MotionEvent.ACTION_CANCEL:
			case MotionEvent.ACTION_UP:
				pressed = false;
				postInvalidate();
				break;
			default:
				break;
			}
		}
		return touched;
	}
	
	public float getBorderWidth() {
		return borderWidth;
	}
}
